﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace 新门禁.Utils
{
    public static class TcpRequestUtil
    {
        public static void Main(String type)//连接dido的ip
        {
            // 服务器IP地址和端口
            string didoIp = App.GetBaseInfo().DIDO_IP;
            IPAddress serverIp = IPAddress.Parse(didoIp);
            int port = 1030;
            //int port = 502;
            //int port = 4000;

            // 创建TcpClient实例
            using (TcpClient client = new TcpClient())
            {
                // 连接到服务器
                client.Connect(serverIp, port);

                // 获取网络流
                using (NetworkStream stream = client.GetStream())
                {
                    // 要发送的数据
                    //string data = "Hello, Server!";
                    //byte[] bytesToSend = Encoding.ASCII.GetBytes(data);
                    byte[] bytes = new byte[10];
                    bytes[0] = 0x48;
                    bytes[1] = 0x3A;
                    bytes[2] = 0x01;
                    bytes[3] = 0x70;
                    if ("in".Equals(type))
                    {
                        bytes[4] = 0x01;
                    }
                    else if ("out".Equals(type))
                    {
                        bytes[4] = 0x01;
                    }
                    //else if ("3".Equals(type))
                    //{
                    //    bytes[4] = 0x03;
                    //}
                    //else if ("4".Equals(type))
                    //{
                    //    bytes[4] = 0x04;
                    //}
                    bytes[5] = 0x01;
                    bytes[6] = 0x00;
                    bytes[7] = 0x01;
                    bytes[8] = 0x45;
                    bytes[9] = 0x44;
                    // 发送数据到服务器
                    stream.Write(bytes, 0, bytes.Length);

                    // 接收服务器响应
                    byte[] resBytes = new byte[1024];
                    int bytesRead = stream.Read(resBytes, 0, resBytes.Length);

                    // 将接收到的字节转换成字符串
                    string response = Encoding.ASCII.GetString(bytes, 0, bytesRead);

                    //Console.WriteLine("服务器响应: " + response);
                }
            }
        }
    }
}